<?php

    class Invoicem extends CI_Model {

        var $data;

        function __construct ()
        {
            parent::__construct ();
        }

        public function get_data ()
        {

            return $this->data;
        }

        function prepare_and_send ( $type, $bookid )
        {
            if ( !isset ( $bookid ) )
            {
                echo 'Error ocured.<br /> booking_id is not set.';
                exit ();
            }
            if ( !isset ( $type ) )
            {
                echo 'Error ocured.<br /> booking_type is not set.';
                exit ();
            }

            switch ( $type )
            {

                case 'carbooking':

                    $this->rest->initialize ( array (
                        'server' => REST_URL_RENTACAR
                    ) );

                    //Get Data - informacionisistem.com
                    $params = array ('carbooking_id' => $bookid, 'lang' => $this->lang->lang ());

                    $dbRet = $this->rest->post ( 'show_invoice_rentacar', $params );

//                    $this->firephp->log($dbRet);
                    //return;
                    //test for valid json
                    if ( $dbRet )
                    {
                        //print_r($order[0]);
                        //print_r($order[1]);
                        $order = $dbRet[0];

                        $this->data['invoice_no'] = $order[0]->id;
                        $this->data['cust_firstName'] = $order[0]->firstName;
                        $this->data['cust_email'] = $order[0]->email;
                        $this->data['cust_phone'] = $order[0]->phone;
                        $this->data['cust_address'] = $order[0]->address1;
                        $this->data['cust_city'] = $order[0]->town_city;
                        $this->data['cust_zip'] = $order[0]->zip_code;
                        $this->data['cust_country'] = $order[0]->country;

                        $this->data['invoice_title'] = $order[0]->carname;

                        $this->data['rtcr_invc_vr'] = true;

                        $this->data['titles'][0] = 'Pickup:';
                        $this->data['titles'][1] = 'Return:';

                        $this->data['datas'][0] = $order[0]->pickup_loc . ' <br /> ' . date ( 'd.m.Y / H:s', $order[0]->datefrom ) . ' h';
                        $this->data['datas'][1] = $order[0]->return_loc . ' <br /> ' . date ( 'd.m.Y / H:s', $order[0]->dateto ) . ' h';

                        $this->data['prices'][0] = $order[0]->pickup_loc_price;
                        $this->data['prices'][1] = $order[0]->return_loc_price;

                        $this->data['price_title'] = 'Rentacar:';
                        $this->data['price_desc'] = $order[0]->carname . ' - ' . $order[0]->numofdays . ' day(s)';
                        $this->data['price_day_price'] = $order[0]->dayprice;
                        $this->data['price_total_price'] = $order[0]->dayprice * $order[0]->numofdays;
                        $this->data['total_price'] = $order[0]->totalprice;


                        //check secon row - accessories table
                        $this->data['cb_numofdays'] = $order[0]->numofdays;

                        $order_extras = $this->test_response ( $dbRet[1] );

                        if ( $order_extras )
                        {
                            //print_r($order_extras);
                            $this->data['accesories'] = $order_extras;
                        }
                        else
                        {
                            //echo $this->invoice->getFlag().":";

                            switch ( $this->getFlag () )
                            {
                                case 'error':
                                    echo $this->getError ();
                                    break;
                                case 'numrows':
                                    //echo $this->invoice->getNumrows();
                                    break;
                                case 'info':
                                    echo $this->getInfo ();
                                    break;
                            }
                        }
                    }
                    else
                    {
                        echo 'Booking ' . $this->getFlag () . ":";

                        switch ( $this->getFlag () )
                        {
                            case 'error':
                                echo $this->getError ();
                                break;
                            case 'numrows':
                                echo $this->getNumrows ();
                                break;
                            case 'info':
                                echo $this->getInfo ();
                                break;
                        }
                    }

                    break;

                case 'excursionbooking':

                    $this->rest->initialize ( array (
                        'server' => REST_URL_RENTACAR
                    ) );

                    //Get Data - informacionisistem.com
                    $params = array ('excursionbooking_id' => $bookid);

                    $order = $this->rest->post ( 'show_invoice_exc', $params );

//                    $this->firephp->log($order);
                    //data for view
                    $this->data = $order;
                    $this->data->exc_persons = TRUE;

//                    $this->firephp->log ($this->data);

                    break;

                case 'tourbooking':

                    $this->rest->initialize ( array (
                        'server' => REST_URL_RENTACAR
                    ) );

                    //Get Data - informacionisistem.com
                    $params = array ('tourbooking_id' => $bookid);

                    $order = $this->rest->post ( 'show_invoice_tour', $params );

                    //data for view
                    $this->data = $order;

                    break;

                case 'reservations':

                    $this->rest->initialize ( array (
                        'server' => REST_URL_HOTELS,
                        'http_user' => 'cipa',
                        'http_pass' => 'booking',
                        'http_auth' => 'basic' // or 'digest' 
                    ) );

                    //Get Data - informacionisistem.com
                    $params = array ('reservation_id' => $bookid);

                    $order = $this->rest->post ( 'showReservationInfo', $params );

                    //$this->firephp->log($order);
                    //data for view
                    //$this->data=$order;

                    $this->data['title'] = 'TOURIST VOUCHER #' . $order->reservation->ID;
                    $this->data['access_url'] = 'http://www.informacionisistem.com/accommodation';
                    $this->data['cancellation_link'] = 'http://globtour.me/en/hotels/reservation_cancellation?reservation_ID=' . $order->reservation->ID;

                    $this->data['arrival'] = date ( 'd.m.Y', $order->reservation->start );
                    $this->data['departure'] = date ( 'd.m.Y', $order->reservation->end );
                    $this->data['client'] = $order->passenger->name . ' ' . $order->passenger->surname;
                    $this->data['address'] = $order->passenger->address;
                    $this->data['city'] = $order->passenger->birth_place;
                    $this->data['zip'] = $order->passenger->zip_code;
                    $this->data['country'] = $order->passenger->country;
                    $this->data['persons'] = $order->reservation->nr_persons;
                    $this->data['destination'] = $order->destination_name;
                    $this->data['accommodation'] = $order->object_name . ', ' . $order->object_address;
                    $this->data['contact_email'] = $order->object_contact_email;
                    $this->data['room_type'] = $order->room->name;
                    $this->data['service'] = $order->room->service_name;
                    $this->data['total'] = $order->reservation->price_total;

                    $cards = "";

                    if ( isset ( $order->object_visa ) && $order->object_visa == '1' )
                        $cards = "VISA";

                    if ( isset ( $order->object_master_card ) && $order->object_master_card == '1' )
                    {
                        if ( $cards != "" )
                        {
                            $cards.=', ';
                        }
                        $cards .= "Master Card";
                    }


                    if ( isset ( $order->object_diners_club ) && $order->object_diners_club == '1' )
                    {
                        if ( $cards != "" )
                        {
                            $cards.=', ';
                        }
                        $cards .= "Diners Club";
                    }


                    if ( isset ( $order->object_maestro ) && $order->object_maestro == '1' )
                    {
                        if ( $cards != "" )
                        {
                            $cards.=', ';
                        }
                        $cards .= "Maestro";
                    }

                    $this->data['cards'] = $cards;

                    break;
            }

            if ( $type == 'reservations' )
            {
                $text = $this->load->view ( 'hotels/parts/tourist_voucher', $this->data, TRUE );
            }
            else
            {
                //$this->firephp->log($this->data);
                $text = $this->load->view ( 'layout/showInvoice', $this->data, TRUE );
            }

            // Get customer email address

            if ( $type == 'carbooking' )
            {
                $customer_email_address = $order[0]->email;
            }
            else if ( $type == 'reservations' )
            {
                $customer_email_address = $order->passenger->email;
            }
            else
            {
                $customer_email_address = $order->cust_email;
            }


            //  SEND MAIL TO CUSTOMER                 
            $this->mail_send ( $customer_email_address, $text );

            //  SEND INFO 1 TO AGENCY
            $this->mail_send ( 'dado@globtour.me', $text );
            
            //  SEND INFO 2 TO AGENCY
            $this->mail_send ( 'info@globtour.me', $text );

            //  SEND INFO 3 TO AGENCY
            $this->mail_send ( 'dado.radunovic@gmail.com', $text );

            //  SEND INFO 4 TO AGENCY
            $this->mail_send ( 'dado.globtour@t-com.me', $text );

            //  SEND INFO 5 TO Administrator
            $this->mail_send ( 'ikukic@yahoo.com', $text );

//            echo $text;
        }

        function mail_send ( $to, $message )
        {

            $from = 'no-reply@globtour.me';
            $subject = 'ONLINE BOOKING - Globtour Montenegro';

            $headers = "From: $from\r\n";
            $headers .= "Content-type: text/html\r\n";

            return mail ( $to, $subject, $message, $headers ); //0 or 1
        }

        public function sendHotelVoucher ( $object )
        {

            // Create and send email
            $this->load->library ( 'mail_send' );
            $this->mail_send->setFrom ( 'info@bookmontenegro.me' );
            $this->mail_send->setTo ( $top_passenger['email'] );
            $this->mail_send->setSubject ( 'Book Montenegro | TOURIST VOUCHER' );
            $this->mail_send->setMessage ( $this->load->view ( 'email/tourist_voucher', $data, TRUE ) );
            $this->mail_send->send ();


            $data['title'] = 'NEW RESERVATION';
            $data['guarantee_link'] = 'http://www.sitmontenegro.com/soho/accommodation/guarantee/view_all';
            $this->mail_send->setFrom ( 'info@bookmontenegro.me' );
            $this->mail_send->setTo ( 'ikukic@yahoo.com' );
            $this->mail_send->setSubject ( 'Book Montenegro | NEW RESERVATION' );
            $this->mail_send->setMessage ( $this->load->view ( 'email/agency_info', $data, TRUE ) );
            $this->mail_send->send ();
            ;
        }

        private $error;
        private $info;
        private $numrows;
        private $flag;

        public function getError ()
        {
            return $this->error;
        }

        public function setError ( $value )
        {
            $this->error = $value;
        }

        public function getInfo ()
        {
            return $this->info;
        }

        public function setInfo ( $value )
        {
            $this->info = $value;
        }

        public function getNumrows ()
        {
            return $this->numrows;
        }

        public function setNumrows ( $value )
        {
            $this->numrows = $value;
        }

        public function getFlag ()
        {
            return $this->flag;
        }

        public function setFlag ( $value )
        {
            $this->flag = $value;
        }

        function test_response ( $dbRet )
        {
            //print_r($dbRet);
            if ( !isset ( $dbRet ) )
            {
                $this->setFlag ( 'error' );
                $this->setError ( 'Rest service error!' );
                return FALSE;
            };

            if ( isset ( $dbRet->error ) )
            {
                $this->setFlag ( 'error' );
                $this->setError ( $dbRet->error );
                return FALSE;
            }

            if ( isset ( $dbRet->num_rows ) )
            {
                $this->setFlag ( 'numrows' );
                $this->setNumrows ( $dbRet->num_rows );
                return FALSE;
            }

            if ( isset ( $dbRet->info ) )
            {
                $this->setFlag ( 'info' );
                $this->setInfo ( $dbRet->info );
                return FALSE;
            }

            return $dbRet;
        }

    }

?>
